package com.fdd.op.sdk.internal.util;

import java.util.regex.Pattern;

/**
 * ip工具类
 *
 * @version 2.0.0
 * @author: zhouj1@fadada.com
 * @date: 2022/9/7
 */
public class IpUtils {
    /**
     * IP v4<br>
     * 采用分组方式便于解析地址的每一个段
     */
    private final static String IPV4 = "^(25[0-5]|2[0-4]\\d|[0-1]?\\d?\\d)\\.(25[0-5]|2[0-4]\\d|[0-1]?\\d?\\d)\\.(25[0-5]|2[0-4]\\d|[0-1]?\\d?\\d)\\.(25[0-5]|2[0-4]\\d|[0-1]?\\d?\\d)$";
    /**
     * IP v6
     */
    private final static String IPV6 = "(([0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]+|::(ffff(:0{1,4})?:)?((25[0-5]|(2[0-4]|1?[0-9])?[0-9])\\.){3}(25[0-5]|(2[0-4]|1?[0-9])?[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1?[0-9])?[0-9])\\.){3}(25[0-5]|(2[0-4]|1?[0-9])?[0-9]))";

    /**
     * IP v4
     */
    private final static Pattern PATTERN_IPV4 = Pattern.compile(IPV4);
    /**
     * IP v6
     */
    private final static Pattern PATTERN_IPV6 = Pattern.compile(IPV6);

    /**
     * 验证是否为IPV4地址
     *
     * @param value 值
     * @return 是否为IPV4地址
     */
    public static boolean isIpv4(CharSequence value) {
        return isMatchRegex(PATTERN_IPV4, value);
    }

    /**
     * 验证是否为IPV6地址
     *
     * @param value 值
     * @return 是否为IPV6地址
     */
    public static boolean isIpv6(CharSequence value) {
        return isMatchRegex(PATTERN_IPV6, value);
    }

    /**
     * 通过正则表达式验证
     *
     * @param pattern 正则模式
     * @param value   值
     * @return 是否匹配正则
     */
    private static boolean isMatchRegex(Pattern pattern, CharSequence value) {
        return isMatch(pattern, value);
    }

    /**
     * 给定内容是否匹配正则
     *
     * @param pattern 模式
     * @param content 内容
     * @return 正则为null或者""则不检查，返回true，内容为null返回false
     */
    private static boolean isMatch(Pattern pattern, CharSequence content) {
        if (content == null || pattern == null) {
            // 提供null的字符串为不匹配
            return false;
        }
        return pattern.matcher(content).matches();
    }

    /**
     * 校验ip地址
     *
     * @throws
     * @Author: zhouj1@fadada.com
     * @Date: 2022/9/7
     * @Param: [ips]
     * @Return: java.lang.String
     */
    public static String getIp(String ips) {
        if (StringUtils.isEmpty(ips)) {
            return "";
        }
        if (ips.contains(",")) {
            String[] ipArray = ips.trim().split(",");
            for (int index = 0; index < ipArray.length; index++) {
                String strIp = ipArray[index];
                if (isIpv4(strIp) || isIpv6(strIp)) {
                    return strIp;
                }
            }
        } else {
            if (isIpv4(ips) || isIpv6(ips)) {
                return ips;
            }
        }
        return "";
    }

}
